package net

import (
	"math/rand/v2"
	"smart-flow/server/config"
	"smart-flow/server/logger"
)

var (
	consoleClients []*ConsoleClient
	clientCount    int
)

func InitConsoleClients() {
	clientCount = len(config.AppConfig().Console.Servers)
	if clientCount == 0 {
		logger.Logger.Error("[网络] 没有可用的CONSOLE服务器")
		return
	}
	for _, server := range config.AppConfig().Console.Servers {
		consoleClients = append(consoleClients, new(ConsoleClient).ConnectConsoleServer(server.Ip, server.Port, true))
	}
}

func DisConnectConsoleServers() {
	for _, consoleClient := range consoleClients {
		consoleClient.DisConnectConsoleServer()
	}
}

func GenConsoleIndex() int {
	return rand.Int() % clientCount
}
